package com.cl.master.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @Author xc
 * @Date 2023/3/25 22:13
 * @Version 1.0
 */
@Configuration
public class DruidConfig {
    /*添加bean注解交给spring容器管理*/
    @Bean
    /*绑定yaml文件中前缀是spring.datasource的配置，并且符合松散绑定*/
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource DruidDataSource(){
        return new DruidDataSource();
    }
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        //配置后台登录用户
        HashMap<String, String> initParameters = new HashMap<>();
        //登录的key是固定的
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","admin");
        //设置访问权限
        //允许访问
        initParameters.put("allow","");
        //禁止访问
        //initParameters.put("key","IP地址");
        //初始化参数
        bean.setInitParameters(initParameters);
        return  bean;
    }
}
